<template>
  <div>
    <my-header></my-header>

    <el-card class="card">
      <el-button
        type="primary"
        style="margin-bottom: 20px"
        @click="dialogFormVisible = true"
        >添加</el-button
      >

      <!-- 表格 -->
      <el-table
        :data="list"
        style="width: 100%; margin-bottom: 20px"
        row-key="cat_id"
        border
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column type="index" label="#" width="180"> </el-table-column>
        <el-table-column prop="cat_name" label="分类名称" width="180">
        </el-table-column>
        <el-table-column prop="cat_deleted" label="是否有效" width="180">
          <i
            type="success"
            class="el-icon-success"
            style="color: lightgreen"
            circle
          ></i>
        </el-table-column>
        <el-table-column prop="cat_level" label="排序" width="180">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.cat_level == 0">一级</el-tag>
            <el-tag type="danger" v-if="scope.row.cat_level == 1">二级</el-tag>
            <el-tag type="warning" v-if="scope.row.cat_level == 2">三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
              class="el-icon-edit"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              class="el-icon-delete-solid"
              @click="handleDelete(scope.$index, scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="obj.pagenum"
        :page-sizes="[1, 3, 5, 10, 15, 20, 30, 40, 50]"
        :page-size="obj.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加模态框 -->
    <el-dialog title="添加商品" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="ruleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="value"
            :options="options"
            :props="{
              expandTrigger: 'hover',
              label: 'cat_name',
              value: 'cat_id',
              checkStrictly: true,
            }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog title="修改商品" :visible.sync="dialogFormVisible1">
      <el-form
        :model="ruleForm1"
        ref="ruleForm1"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="ruleForm1.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="submitForm1('ruleForm1')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCate, getAddCate,getPutCate,getDelCate} from "../../api/cate";
import myHeader from "../../components/myHeader.vue";
export default {
  components: { myHeader },
  data() {
    return {
      value: [],
      options: [],
      ruleForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 1,
      },
      dialogFormVisible: false,
      ruleForm1: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 1,
      },
      dialogFormVisible1: false,
      list: [],
      obj: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 添加
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // alert('submit!');
          let res = await getAddCate(this.ruleForm);
          console.log(res, "resAdd");
          if (res.meta.status == 201) {
            this.getList();
            this.dialogFormVisible = false;
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 层级分类
    handleChange(value) {
      console.log(value, "value");
      // 添加市 父级分类
      this.ruleForm.cat_pid = value[value.length - 1];
      // 添加市 层级分类
      this.ruleForm.cat_level = value.length;
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.obj.pagesize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.obj.pagenum = val;
      this.getList();
    },
    // 获取所有数据
    async getList() {
      let res = await getCate(this.obj);
      console.log(res, "resList");
      if (res.meta.status == 200) {
        this.list = res.data.result;
        this.total = res.data.total;
        this.options = res.data.result;
      }
    },
    // 编辑
    handleEdit(index, row) {
      console.log(index, row);
      this.ruleForm1 = row;
      this.dialogFormVisible1 = true;
    },
    submitForm1(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // alert('submit!');
          let po={id:this.ruleForm1.cat_id,cat_name:this.ruleForm1.cat_name}
          console.log(this.ruleForm1,"ruleForm1");
          let res = await getPutCate(po);
          console.log(res, "resPut");
          if (res.meta.status == 200) {
            this.getList();
            this.dialogFormVisible1 = false;
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 删除
    handleDelete(index, row) {
      console.log(index, row);
              this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async  () => {
            let res=await getDelCate(row)
            console.log(res,"resDel")
            this.getList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
  },
};
</script>

<style scoped>
.card {
  margin-top: 20px;
}
</style>